// 配置RequireJS超时时间
require.config({
    waitSeconds: 30
});

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/trace_code/index' + location.search,
                    del_url: 'scanwork/trace_code/del',
                    multi_url: 'scanwork/trace_code/multi',
                    table: 'trace_code',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'trace_code', title: '追溯码', width: 120},
                        {field: 'order_no', title: '订单号', width: 120},
                        {field: 'model_name', title: '产品型号', width: 120},
                        {field: 'model_code', title: '型号编码', width: 120},
                        {field: 'process_name', title: '工序名称', width: 100},
                        {field: 'worker_name', title: '提交员工', width: 100},
                        {field: 'auditor_name', title: '审核员工', width: 100},
                        {field: 'quantity', title: '报工数量', width: 80},
                        {field: 'item_nos', title: '产品编号', width: 150},
                        {field: 'remark', title: '备注', width: 120},
                        {field: 'audit_reason', title: '审核备注', width: 120},
                        {field: 'audit_time', title: '审核时间', width: 150, formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), width: 80, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('创建时间'), width: 150, formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('操作'), table: table, events: {
                            'click .btn-detail': function (e, value, row, index) {
                                e.stopPropagation();
                                window.open('detail?id=' + row.id, '_blank');
                            },
                            'click .btn-preview': function (e, value, row, index) {
                                e.stopPropagation();
                                window.open('preview?id=' + row.id, '_blank');
                            }
                        }, formatter: function (value, row, index) {
                            var buttons = [];
                            buttons.push('<a href="javascript:;" class="btn btn-xs btn-info btn-detail" title="详情"><i class="fa fa-eye"></i> 详情</a> ');
                            buttons.push('<a href="javascript:;" class="btn btn-xs btn-success btn-preview" title="预览"><i class="fa fa-qrcode"></i> 预览</a> ');
                            buttons.push('<a href="del/ids/' + row.id + '" class="btn btn-xs btn-danger btn-delone" title="删除"><i class="fa fa-trash"></i> 删除</a> ');
                            return buttons.join(' ');
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 绑定自定义按钮事件
            $(document).on('click', '.btn-generate', function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.error('请选择要生成追溯码的报工记录');
                    return;
                }
                Fast.api.ajax({
                    url: 'scanwork/trace_code/generate',
                    data: {ids: ids.join(',')}
                }, function (data, ret) {
                    table.bootstrapTable('refresh');
                    return false;
                });
            });

            $(document).on('click', '.btn-download-qr', function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.error('请选择要下载的追溯码');
                    return;
                }
                var url = 'downloadQrCodeNew?ids=' + ids.join(',');
                window.open(url, '_blank');
            });

            // 绑定打印追溯码事件
            $(document).on('click', '.btn-print-qr', function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.error('请选择要打印的追溯码');
                    return;
                }
                var url = 'printA4?ids=' + ids.join(',');
                window.open(url, '_blank');
            });

            // 绑定批量生成历史追溯码事件
            $(document).on('click', '[data-url*="generateAll"]', function () {
                var url = $(this).data('url');
                Layer.confirm('确定要为所有历史报工记录生成追溯码吗？这将处理所有已审核的报工数据。', {
                    title: '批量生成追溯码',
                    btn: ['确定', '取消']
                }, function () {
                    Fast.api.ajax({
                        url: url
                    }, function (data, ret) {
                        table.bootstrapTable('refresh');
                        return false;
                    });
                });
            });

            // 绑定清理过期二维码事件
            $(document).on('click', '.btn-clean-qr', function () {
                Layer.confirm('确定要清理过期的二维码文件吗？这将删除7天前的二维码文件。', {
                    title: '清理过期二维码',
                    btn: ['确定', '取消']
                }, function () {
                    Fast.api.ajax({
                        url: 'scanwork/trace_code/cleanExpiredQrCodes'
                    }, function (data, ret) {
                        Toastr.success(data.message);
                        return false;
                    });
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
